//
//  ProfileViewController.m
//  CangLongWnnianli
//
//  Created by 暗流澎涌 on 2025/7/2.
//

#import "ProfileViewController.h"
#import <Masonry/Masonry.h>
#import <WebKit/WebKit.h>
#import <objc/runtime.h>

@interface ProfileViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *settingsButton;

@end

@implementation ProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    // 顶部导航栏
    UIView *navBar = [[UIView alloc] init];
     // 主题色
    navBar.backgroundColor = [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0];
    [self.view addSubview:navBar];
    [navBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(88); // 状态栏+导航栏
    }];

    // 返回按钮
//    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [backBtn setTitle:@"<" forState:UIControlStateNormal];
//    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    backBtn.titleLabel.font = [UIFont systemFontOfSize:28];
//    [backBtn addTarget:self action:@selector(backBtnTapped) forControlEvents:UIControlEventTouchUpInside];
//    [navBar addSubview:backBtn];
//    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(navBar).offset(16);
//        make.bottom.equalTo(navBar).offset(-12);
//        make.width.height.mas_equalTo(32);
//    }];

    // 标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"我的";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor whiteColor];
    [navBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(navBar);
        make.top.equalTo(navBar.mas_top).offset(60);
        
    }];

    // VIP卡片
//    UIView *vipCard = [[UIView alloc] init];
//    vipCard.backgroundColor = [UIColor whiteColor];
//    vipCard.layer.cornerRadius = 20;
//    [self.view addSubview:vipCard];
//    [vipCard mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(navBar.mas_bottom).offset(24);
//        make.left.right.equalTo(self.view).inset(20);
//        make.height.mas_equalTo(120);
//    }];
//
//    // VIP图标
//    UILabel *vipIcon = [[UILabel alloc] init];
//    vipIcon.text = @"👑";
//    vipIcon.font = [UIFont systemFontOfSize:32];
//    [vipCard addSubview:vipIcon];
//    [vipIcon mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.equalTo(vipCard).offset(16);
//    }];
//
//    // 成为VIP按钮
//    UIButton *vipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [vipBtn setTitle:@"成为VIP" forState:UIControlStateNormal];
//    [vipBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    vipBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
//    vipBtn.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
//    vipBtn.layer.cornerRadius = 28;
//    [vipCard addSubview:vipBtn];
//    [vipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(vipCard);
//        make.centerX.equalTo(vipCard);
//        make.height.mas_equalTo(56);
//        make.width.mas_equalTo(180);
//    }];
//    [vipBtn addTarget:self action:@selector(vipBtnTapped) forControlEvents:UIControlEventTouchUpInside];

    // 菜单卡片
    UIView *menuCard = [[UIView alloc] init];
    menuCard.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    menuCard.layer.cornerRadius = 20;
    [self.view addSubview:menuCard];
    [menuCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navBar.mas_bottom).offset(24);
        make.left.right.equalTo(self.view).inset(20);
        make.height.mas_equalTo(120);
    }];

    // 隐私政策按钮
    UIButton *privacyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [privacyBtn setTitle:@"隐私政策" forState:UIControlStateNormal];
    [privacyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    privacyBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    privacyBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    privacyBtn.tag = 0;
    [privacyBtn addTarget:self action:@selector(menuItemTapped:) forControlEvents:UIControlEventTouchUpInside];
    [menuCard addSubview:privacyBtn];
    [privacyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(menuCard).offset(20);
        make.right.equalTo(menuCard).offset(-40);
        make.top.equalTo(menuCard).offset(0);
        make.height.mas_equalTo(60);
    }];

    // 箭头1
    UIImageView *arrow1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next"]];
    arrow1.tintColor = [UIColor lightGrayColor];
    [menuCard addSubview:arrow1];
    [arrow1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(privacyBtn);
        make.right.equalTo(menuCard).offset(-16);
        make.height.mas_equalTo(18);
        make.width.mas_equalTo(10);
    }];

    // 分割线
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    [menuCard addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(menuCard).inset(16);
        make.top.equalTo(privacyBtn.mas_bottom);
        make.height.mas_equalTo(1);
    }];

    // 用户协议按钮
    UIButton *userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [userBtn setTitle:@"用户协议" forState:UIControlStateNormal];
    [userBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    userBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    userBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    userBtn.tag = 1;
    [userBtn addTarget:self action:@selector(menuItemTapped:) forControlEvents:UIControlEventTouchUpInside];
    [menuCard addSubview:userBtn];
    [userBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(menuCard).offset(20);
        make.right.equalTo(menuCard).offset(-40);
        make.top.equalTo(line.mas_bottom);
        make.height.mas_equalTo(60);
    }];

    // 箭头2
    UIImageView *arrow2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next"]];
    arrow2.tintColor = [UIColor lightGrayColor];
    [menuCard addSubview:arrow2];
    [arrow2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(userBtn);
        make.right.equalTo(menuCard).offset(-16);
        make.height.mas_equalTo(18);
        make.width.mas_equalTo(10);
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"ProfileViewController viewDidAppear");
    
    // 检查测试按钮是否还在
    UIButton *testButton = [self.view viewWithTag:888];
    if (testButton) {
        NSLog(@"测试按钮存在，frame: %@", NSStringFromCGRect(testButton.frame));
        NSLog(@"测试按钮 userInteractionEnabled: %@", testButton.userInteractionEnabled ? @"YES" : @"NO");
    } else {
        NSLog(@"测试按钮不存在！");
    }
    
    // 检查self.view的状态
    NSLog(@"self.view userInteractionEnabled: %@", self.view.userInteractionEnabled ? @"YES" : @"NO");
    NSLog(@"self.view frame: %@", NSStringFromCGRect(self.view.frame));
    
    // 尝试直接调用方法
    NSLog(@"尝试直接调用 testButtonTapped");
    [self testButtonTapped:testButton];
    
    // 检查菜单按钮
    UIButton *menuButton0 = [self.view viewWithTag:0];
    UIButton *menuButton1 = [self.view viewWithTag:1];
    
    if (menuButton0) {
        NSLog(@"菜单按钮0存在，frame: %@", NSStringFromCGRect(menuButton0.frame));
        NSLog(@"菜单按钮0 userInteractionEnabled: %@", menuButton0.userInteractionEnabled ? @"YES" : @"NO");
    } else {
        NSLog(@"菜单按钮0不存在！");
    }
    
    if (menuButton1) {
        NSLog(@"菜单按钮1存在，frame: %@", NSStringFromCGRect(menuButton1.frame));
        NSLog(@"菜单按钮1 userInteractionEnabled: %@", menuButton1.userInteractionEnabled ? @"YES" : @"NO");
    } else {
        NSLog(@"菜单按钮1不存在！");
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

#pragma mark - Actions

- (void)settingsButtonTapped {
    NSLog(@"设置按钮被点击");
    // TODO: 实现设置页面
}

- (void)editProfileTapped {
    NSLog(@"编辑资料被点击");
    // TODO: 实现编辑资料页面
}

- (void)testButtonTapped:(id)sender {
    NSLog(@"=== 测试按钮被点击 ===");
    NSLog(@"方法被调用，sender: %@", sender);
    
    UIButton *button = (UIButton *)sender;
    NSLog(@"测试按钮 tag: %ld", (long)button.tag);
    
    // 先尝试简单的 NSLog
    NSLog(@"按钮点击成功！");
    
}

- (void)menuItemTapped:(id)sender {
    NSLog(@"=== menuItemTapped 被调用 ===");
    NSLog(@"sender: %@", sender);
    NSLog(@"sender 类型: %@", NSStringFromClass([sender class]));
    
    NSInteger index = -1;
    
    if ([sender isKindOfClass:[UIButton class]]) {
        index = ((UIButton *)sender).tag;
        NSLog(@"按钮点击，tag: %ld", (long)index);
    } else if ([sender isKindOfClass:[UITapGestureRecognizer class]]) {
        UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;
        index = tap.view.tag;
        NSLog(@"手势点击，view tag: %ld", (long)index);
    } else {
        NSLog(@"未知的sender类型");
    }
    
    NSLog(@"菜单项被点击: %ld", (long)index);
    
    switch (index) {
        case 0: // 隐私政策
            NSLog(@"准备显示隐私政策");
            [self showPrivacyPolicy];
            break;
        case 1: // 用户协议
            NSLog(@"准备显示用户协议");
            [self showUserAgreement];
            break;
        default:
            NSLog(@"未知的菜单项索引: %ld", (long)index);
            break;
    }
}

- (void)logoutButtonTapped {
    NSLog(@"退出登录被点击");
    // TODO: 实现退出登录逻辑
}

#pragma mark - Menu Actions

- (void)showPrivacyPolicy {
    NSLog(@"显示隐私政策页面");
    
    
    // 注释掉WebView测试
     [self showWebViewWithTitle:@"隐私政策" url:@"https://rs6s8e.xiaoyu233.cc/privacy_policy.html"];
}

- (void)showUserAgreement {
    NSLog(@"显示用户协议页面");
    
   
    // 注释掉WebView测试
     [self showWebViewWithTitle:@"用户协议" url:@"https://rs6s8e.xiaoyu233.cc/service_agreement.html"];
}

#pragma mark - WebView Helper

- (void)showWebViewWithTitle:(NSString *)title url:(NSString *)urlString {
    // 创建WebView控制器
    UIViewController *webVC = [[UIViewController alloc] init];
    webVC.title = title;
    webVC.view.backgroundColor = [UIColor whiteColor];
    
    // 顶部自定义导航栏
    UIView *navBar = [[UIView alloc] init];
     navBar.backgroundColor = [UIColor colorWithRed:0.773 green:0.188 blue:0.188 alpha:1.0];
    [webVC.view addSubview:navBar];
    [navBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(webVC.view);
        make.height.mas_equalTo(88);
    }];
    
    // 返回按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:webVC action:@selector(dismissSelf) forControlEvents:UIControlEventTouchUpInside];
    [navBar addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(navBar).offset(12);
        make.centerY.equalTo(navBar).offset(20);
        make.width.height.mas_equalTo(32);
    }];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textColor = [UIColor blackColor];
    [navBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(navBar);
        make.centerY.equalTo(navBar).offset(20);
    }];
    
    // WKWebView
    WKWebView *webView = [[WKWebView alloc] init];
    [webVC.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navBar.mas_bottom);
        make.left.right.bottom.equalTo(webVC.view);
    }];
    if (urlString) {
        NSURL *url = [NSURL URLWithString:urlString];
        if (url) {
            [webView loadRequest:[NSURLRequest requestWithURL:url]];
        }
    }
    
    // 给webVC动态添加dismissSelf方法
    class_addMethod([webVC class], @selector(dismissSelf), (IMP)dismissSelfIMP, "v@:");
    
    webVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:webVC animated:YES completion:nil];
}

// 动态添加的返回方法实现
void dismissSelfIMP(id self, SEL _cmd) {
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 返回按钮事件
- (void)backBtnTapped {
    [self.navigationController popViewControllerAnimated:YES];
}

// 成为VIP按钮事件
- (void)vipBtnTapped {
    // TODO: 实现VIP功能
    NSLog(@"成为VIP按钮被点击");
}

@end 
